package cn.com.nes.site.service.equipment.archives;

import cn.com.nes.mybatis.agent.ope.entity.IsEquipmentInfo;
import cn.com.nes.mybatis.agent.ope.entity.IsInstallInfo;
import cn.com.nes.mybatis.agent.ope.mapper.IsEquipmentInfoMapper;
import cn.com.nes.mybatis.agent.ope.mapper.IsInstallInfoMapper;
import cn.com.nes.mybatis.agent.ope.mapper.IsWxsbMapper;
import cn.com.nes.site.entity.bo.ope.EquipmentInfoSaveBo;
import cn.com.nes.site.entity.bo.ope.InstallInfoSaveBo;
import cn.com.nes.site.entity.bo.ope.OpeEquipmentArchivesBo;
import cn.com.nes.site.entity.dto.DtuBindInfoDto;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 运营平台设备档案处理类
 */
@Slf4j
@Service
public class OpeEquipmentArchivesHandle {

    @Resource
    private IsInstallInfoMapper isInstallInfoMapper;

    @Resource
    private IsEquipmentInfoMapper isEquipmentInfoMapper;

    @Resource
    private IsWxsbMapper isWxsbMapper;

    /**
     * 保存运营数据设备档案
     * @param bo    运营设备档案业务模型
     * @throws Exception
     */
    public void saveEquipmentArchives(OpeEquipmentArchivesBo bo) throws Exception {
        // 保存安装信息
        saveInstall(bo);

        // 保存设备信息
        saveEquipment(bo);
    }

    /**
     * 保存安装信息
     * @param bo 运营设备档案业务模型
     */
    private void saveInstall(OpeEquipmentArchivesBo bo) throws Exception {
        InstallInfoSaveBo installInfoSaveBo = bo.toInstallInfoSaveBo();

        // 通过ddId，查询数据库中是否存在安装信息，查询到，将主键Id赋值
        IsInstallInfo existInstall = isInstallInfoMapper.getInstallInfo(bo.getDdId());
        if (ObjectUtil.isNotNull(existInstall)) {
            existInstall.setId(existInstall.getId());
        }

        // 保存安装信息入库
        saveInstallToDb(installInfoSaveBo);
    }

    /**
     * 保存安装信息入库
     * @param bo 安装地点信息保存业务模型
     * @return
     */
    private IsInstallInfo saveInstallToDb(InstallInfoSaveBo bo) throws Exception {
        IsInstallInfo installInfo = bo.toInstallInfoDo();
        // todo IsInstallInfo 中增加新增 或 修改 判断方法，返回 boolean， 下边的逻辑根据返回结果，进行判断
        if (ObjectUtil.isNull(installInfo.getId())) {
            installInfo.setCjsj(new Date());
            installInfo.setSfsc(DeleteEnum._0.getKey());
            isInstallInfoMapper.insert(installInfo);
        } else {
            installInfo.setXgsj(new Date());
            isInstallInfoMapper.updateById(installInfo);
        }
        return installInfo;
    }

    /**
     * 保存设备信息
     * @param bo 运营设备档案业务模型
     */
    private void saveEquipment(OpeEquipmentArchivesBo bo) throws Exception {
        List<EquipmentInfoSaveBo> equipmentInfoSaveBos = bo.toEquipmentInfoSaveBo();

        if (CollUtil.isEmpty(equipmentInfoSaveBos)) {
            return;
        }

        for (EquipmentInfoSaveBo equipmentInfoSaveBo : equipmentInfoSaveBos) {
            // 先根据ddId和sbId 查询是否存在设备
            IsEquipmentInfo existEquipment = isEquipmentInfoMapper.getEquipmentInfoUnDelete(equipmentInfoSaveBo.getSbId());
            if (ObjectUtil.isNotNull(existEquipment)) {
                equipmentInfoSaveBo.setId(existEquipment.getId());   // 如存在，设置ID。
            }

            // 如果传入dtuId，dtu是否符合绑定条件
            if (StrUtil.isNotEmpty(equipmentInfoSaveBo.getWxId())) {
                DtuBindInfoDto dtuBindInfoDto = isWxsbMapper.getDtuBindInfo(equipmentInfoSaveBo.getWxId());
                if (ObjectUtil.isNull(dtuBindInfoDto)) {
                    throw new Exception("没有查询到该无线设备信息");
                }
                if (1 == dtuBindInfoDto.getBindStatus()) {
                    throw new Exception("该无线模块已经绑定");
                }
                if (!bo.getManufactureId().equals(dtuBindInfoDto.getManufactureId())) {
                    throw new Exception("该无线模块不属于当前设备公司");
                }
                equipmentInfoSaveBo.setProtocolId(dtuBindInfoDto.getProtocolId());
            }

            // 保存设备信息入库
            saveEquipmentToDb(equipmentInfoSaveBo);
        }
    }

    /**
     * 保存设备信息入库
     * @param bo    设备信息保存业务模型
     * @return
     * @throws Exception
     */
    private IsEquipmentInfo saveEquipmentToDb(EquipmentInfoSaveBo bo) throws Exception {
        IsEquipmentInfo equipmentInfo = bo.convertToEquipmentInfoDo();
        if (ObjectUtil.isNull(equipmentInfo.getId())) {
            isEquipmentInfoMapper.insert(equipmentInfo);
        } else {
            isEquipmentInfoMapper.updateById(equipmentInfo);
        }
        return equipmentInfo;
    }




}
